home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 8 code / TValidText / UValidText.inc1.p < prev    next >
Encoding:
Text File  |  1991-10-09  |  9.0 KB  |  361 lines  |  [TEXT/MPS ]

  1. {*******************************************************************************
  2. UValidText.inc1.p
  3.     TValidText is a class implementing the ability to enter strings which must
  4.     be validated, such as dates, times, numbers, and so on.
  5. *******************************************************************************}
  6.  
  7.  
  8. {###############################################################################
  9. Global Routines
  10. ###############################################################################}
  11.  
  12. {------------------------------------------------------------------------------}
  13. {$S AInit}
  14.                         
  15. PROCEDURE InitUValidText;
  16.     VAR
  17.         dummy:        BOOLEAN;
  18.     
  19.     BEGIN
  20.     IF gDeadStripSuppression
  21.     THEN
  22.         BEGIN
  23.         { view classes }
  24.         dummy := Member(TObject(NIL), TValidText);
  25.         END;  { then }
  26.     END;  { InitUValidText }
  27.  
  28.  
  29. {###############################################################################
  30. TValidText
  31. ###############################################################################}
  32.  
  33. {------------------------------------------------------------------------------}
  34. {$S DlgOpen}
  35.  
  36. PROCEDURE TValidText.IValidText(
  37.                         itsSuperView:    TView;
  38.                         itsLocation:    VPoint;
  39.                         itsSize:        VPoint;
  40.                         strict:            BOOLEAN;
  41.                         required:        BOOLEAN;
  42.                         alertID:        INTEGER);
  43.     
  44.     BEGIN
  45.     IEditText(itsSuperView, itsLocation, itsSize, 255);
  46.     
  47.     fStrict      := strict;
  48.     fRequired := required;
  49.     fAlertID  := alertID;
  50.     END;  { IValidText }
  51.  
  52. {------------------------------------------------------------------------------}
  53. {$S DlgOpen}
  54.  
  55. PROCEDURE TValidText.IRes(
  56.                         itsDocument:    TDocument;
  57.                         itsSuperView:    TView;
  58.                     VAR itsParams:        Ptr);
  59.                         OVERRIDE;
  60.     VAR
  61.         theText:        Str255;
  62.     
  63.     BEGIN
  64.     INHERITED IRes(itsDocument, itsSuperView, itsParams);
  65.     
  66.     { read extra field from resource }
  67.     WITH ValidTextTemplatePtr(itsParams)^ DO
  68.         BEGIN
  69.         fStrict      := strict;
  70.         fRequired := required;
  71.         fAlertID:= alertID;
  72.         END;
  73.  
  74.     { offset the itsParam ptr accordingly }
  75.     OffsetPtr(itsParams, SIZEOF(ValidTextTemplate));
  76.     END;  { IRes }
  77.  
  78.  
  79. {------------------------------------------------------------------------------}
  80. {$S MAWriteRes}
  81.  
  82. PROCEDURE TValidText.WRes(
  83.                         theResource:    ViewRsrcHndl;
  84.                     VAR    itsParams:        Ptr);
  85.                         OVERRIDE;
  86.  
  87.     VAR
  88.         p:                ValidTextTemplatePtr;
  89.  
  90.     BEGIN
  91.     INHERITED WRes(theResource, itsParams);
  92.     
  93.     p := ValidTextTemplatePtr(ExpandPtr(theResource,
  94.                                        itsParams,
  95.                                        SIZEOF(ValidTextTemplate)));
  96.     
  97.     WITH p^ DO
  98.         BEGIN
  99.         strict     := fStrict;
  100.         required := fRequired;
  101.         END;
  102.     END;  { WRes }
  103.  
  104.  
  105. {------------------------------------------------------------------------------}
  106. {$S MAWriteRes}
  107.  
  108. PROCEDURE TValidText.WriteRes(
  109.                         theResource:    ViewRsrcHndl;
  110.                     VAR itsParams:        Ptr);
  111.                         OVERRIDE;
  112.  
  113.     BEGIN
  114.     gWResSignature := 'vTxt';    { See ViewResourceTypes.r }
  115.     gWResType := 'TValidText';
  116.     WRes(theResource, itsParams);
  117.     END;  { WriteRes }
  118.  
  119. {------------------------------------------------------------------------------}
  120. {$S DlgNonRes}
  121.         
  122. PROCEDURE TValidText.SetStrict(
  123.                         strict:            BOOLEAN);
  124.     
  125.     BEGIN
  126.     fStrict := strict;
  127.     END;  { SetStrict }
  128.  
  129. {------------------------------------------------------------------------------}
  130. {$S DlgNonRes}
  131.         
  132. PROCEDURE TValidText.SetRequired(
  133.                         required:        BOOLEAN);
  134.     
  135.     BEGIN
  136.     fRequired := required;
  137.     END;  { SetRequired }
  138.  
  139. {------------------------------------------------------------------------------}
  140. {$S DlgRes}
  141.         
  142. FUNCTION  TValidText.GetStrict
  143.                         :BOOLEAN;
  144.     BEGIN
  145.     GetStrict := fStrict;
  146.     END;  { GetStrict }
  147.  
  148. {------------------------------------------------------------------------------}
  149. {$S DlgRes}
  150.  
  151. FUNCTION  TValidText.GetRequired
  152.                         :BOOLEAN;
  153.     BEGIN
  154.     GetRequired := fRequired;
  155.     END;  { GetRequired }
  156.  
  157. {------------------------------------------------------------------------------}
  158. {$S DlgNonRes}
  159.         
  160. PROCEDURE TValidText.UpdateText(
  161.                         redraw:            BOOLEAN);
  162.     { This routine replaces the text in the object with text that
  163.         reflects the object's current values. }
  164.     BEGIN
  165.     {$IFC qDebug}
  166.     ProgramBreak('The routine TValidText.UpdateText) MUST BE OVERRIDEN!');
  167.     {$ENDC qDebug}
  168.     END;  { UpdateText }
  169.  
  170. {------------------------------------------------------------------------------}
  171. {$S DlgNonRes}
  172.  
  173. FUNCTION  TValidText.IsValid(
  174.                     VAR    theText:        Str255;
  175.                     VAR    whyNot:            INTEGER)
  176.                         :BOOLEAN;
  177.     { If the text is valid, then this function returns TRUE, and whyNot
  178.         is set to the value noErr (0).  If the text is invalid, then the
  179.         function returns FALSE and whyNot is set to a value indicating
  180.         the reason why the text is invalid. }
  181.     BEGIN
  182.     IF fRequired &
  183.        (Length(theText) = 0)
  184.     THEN
  185.         BEGIN
  186.         IsValid := FALSE;
  187.         whyNot  := kEmptyStringError;
  188.         END
  189.     ELSE
  190.         BEGIN
  191.         IsValid := TRUE;
  192.         whyNot    := noErr;
  193.         END;
  194.     END;  { IsValid }
  195.  
  196. {------------------------------------------------------------------------------}
  197. {$S DlgNonRes}
  198.  
  199. FUNCTION  TValidText.HandleValidText(
  200.                     VAR    theText:        Str255)
  201.                         :LONGINT;
  202.     BEGIN
  203.     { tell SELF's superview that SELF's text validated OK }
  204.     fSuperView.DoChoice(SELF, mTextValidated);
  205.     
  206.     HandleValidText := kValidValue;
  207.     END;  { HandleValidText }
  208.  
  209. {------------------------------------------------------------------------------}
  210. {$S DlgNonRes}
  211.  
  212. FUNCTION  TValidText.HandleInvalidText(
  213.                     VAR    theText:        Str255;
  214.                         theError:        INTEGER)
  215.                         :LONGINT;
  216.     BEGIN
  217.     IF ValidationErrorAlert(theText, theError)
  218.     THEN
  219.         HandleInvalidText := HandleAlertAccepted(theText, theError)
  220.     ELSE
  221.         HandleInvalidText := HandleAlertCancelled(theText, theError);
  222.     END;  { HandleInvalidText }
  223.  
  224. {------------------------------------------------------------------------------}
  225. {$S DlgNonRes}
  226.  
  227. FUNCTION  TValidText.HandleAlertAccepted(
  228.                     VAR    theText:        Str255;
  229.                         theError:        INTEGER)
  230.                         :LONGINT;
  231.     { This routine always returns kErrorHandled. Its overrides might
  232.       do something different. }
  233.     BEGIN
  234.     HandleAlertAccepted := kErrorHandled;
  235.     END;  { HandleAlertAccepted }
  236.  
  237. {------------------------------------------------------------------------------}
  238. {$S DlgNonRes}
  239.  
  240. FUNCTION  TValidText.HandleAlertCancelled(
  241.                     VAR    theText:        Str255;
  242.                         theError:        INTEGER)
  243.                         :LONGINT;
  244.     { This routine always returns kErrorHandled. Its overrides might
  245.       do something different. }
  246.     BEGIN
  247.     HandleAlertCancelled := kErrorHandled;
  248.     END;  { HandleAlertCancelled }
  249.  
  250. {------------------------------------------------------------------------------}
  251. {$S DlgNonRes}
  252.  
  253. PROCEDURE TValidText.ErrorToString(
  254.                         theError:        INTEGER;
  255.                     VAR    theString:        Str255);
  256.     { This routine sets theString to the string which best explains
  257.         the given error.  It is intended to be called only from
  258.         ValidationErrorAlert(). }
  259.     BEGIN
  260.     IF (theError = kEmptyStringError)
  261.     THEN
  262.         GetIndString(theString, kValidTextErrorStrings, kEmptyStringErrorStr)
  263.     ELSE
  264.         GetIndString(theString, kInvalidValueReasons, kInvalidValue);
  265.     
  266.     (*
  267.     {$IFC qDebug}
  268.     writeln;
  269.     write('In TValidText.ErrorToString(), theString = "',
  270.           theString, '" and theError = ');
  271.     WriteHexInt(theError);
  272.     writeln;
  273.     {$ENDC qDebug}
  274.     *)
  275.     END;  { ErrorToString }
  276.  
  277. {------------------------------------------------------------------------------}
  278. {$S DlgNonRes}
  279.  
  280. PROCEDURE TValidText.PrepareErrorAlert(
  281.                     VAR    theText:        Str255;
  282.                         theError:        INTEGER);
  283.     { The routine sets up the dialog that is displayed by
  284.         ValidationErrorAlert(), below. }
  285.     VAR
  286.         theString:        Str255;
  287.     
  288.     BEGIN
  289.     { get the best string to describe the given error }
  290.     ErrorToString(theError, theString);
  291.     ParamText(theString, '', '', '');
  292.     END;  { PrepareErrorAlert }
  293.  
  294. {------------------------------------------------------------------------------}
  295. {$S DlgNonRes}
  296.  
  297. FUNCTION  TValidText.ValidationErrorAlert(
  298.                     VAR    theText:        Str255;
  299.                         theError:        INTEGER)
  300.                         : BOOLEAN;
  301.     { This routine displays the standard MacApp validation error alert.
  302.         The function result (true by default) indicates whether or not
  303.         the alert was accepted (true) or cancelled (false). }
  304.     BEGIN
  305.     { Display the alert, and return the result }
  306.     PrepareErrorAlert(theText, theError);
  307.     ValidationErrorAlert := (MacAppAlert(fAlertID, NIL) = ok);
  308.     END;  { ValidationErrorAlert }
  309.  
  310. {------------------------------------------------------------------------------}
  311. {$S DlgNonRes}
  312.  
  313. FUNCTION  TValidText.Validate
  314.                         :LONGINT;
  315.                         OVERRIDE;
  316.     
  317.     VAR
  318.         parentResult:    LONGINT;
  319.         theText:        Str255;
  320.         whyNot:            INTEGER;
  321.     
  322.     BEGIN
  323.     { make sure the current text passes the superclass' validation }
  324.     parentResult := INHERITED Validate;
  325.     
  326.     IF (parentResult <> kValidValue)
  327.     THEN
  328.         Validate := parentResult
  329.     ELSE
  330.         BEGIN
  331.         GetText(theText);
  332.         
  333.         IF IsValid(theText, whyNot)
  334.         THEN
  335.             Validate := HandleValidText(theText)
  336.         ELSE
  337.             Validate := HandleInvalidText(theText, whyNot);
  338.         END;
  339.     END;  { Validate }
  340.  
  341. {------------------------------------------------------------------------------}
  342. {$S DlgFields}
  343.                         
  344. {$IFC qInspector}
  345. PROCEDURE TValidText.Fields(
  346.                         PROCEDURE DoToField(
  347.                             fieldName:        Str255;
  348.                             fieldAddr:        Ptr;
  349.                             fieldType:        INTEGER));
  350.                         OVERRIDE;
  351.         
  352.     BEGIN
  353.     DoToField('TValidText', NIL, bClass);
  354.     DoToField('fStrict', @fStrict, bBoolean);
  355.     DoToField('fRequired', @fRequired, bBoolean);
  356.  
  357.     INHERITED Fields(DoToField);
  358.     END;  { Fields }
  359. {$ENDC qDebug}
  360.  
  361. {------------------------------------------------------------------------------}